#include "Logbook.h"

Logbook::Logbook()
{
	// Load the logbook data
	Loader < Plane > plane_loader("Loading plane data. . .", "input_planes.txt", _planes);
	Loader < Airport > airport_loader("Loading airport data. . .", "input_airports.txt", _airports);
	Loader < Student > student_loader("Loading student data. . .", "input_students.txt", _students);
	Loader < int > date_loader("input_dates.txt", _dates);
	Loader < Entry > entry_loader("Loading entry data. . .", "logbook.csv", _entries, 1);

}

void Logbook::execute(char** argv)
{
	bool all = false;
	if (strcmpi( "-a", argv[1]) == 0)
		all = true;

	// Prompt to log a new entry
	if (!all)
	{
		LogEntry logEntry;
		logEntry.prompt();
	}
}

// Sometimes two smaller for loops are faster than one.
// Most of the time two smaller for loops are easier to read than one. 
void Logbook::verifyPlanes()
{
	// Check the type of each plane
	for (unsigned int i = 1; i < _planes.size(); ++i)
	{
		bool match = false;

		for (unsigned int j = 0; j < _types.size(); ++j)
			if (_planes[i].type() == _types[j].name())
				match = true;

		if (!match)			// No match, create new type
			_types.push_back( Type(_planes[i].type()) );
	}
	
	// Check the model of each plane
	for (unsigned int i = 1; i < _planes.size(); ++i)
	{
		bool match = false;

		for (unsigned int j = 0; j < _models.size(); ++j)
			if (_planes[i].model() == _models[j].name())
				match = true;

		if (!match)				// No match, create new model
			_models.push_back( Model(_planes[i].type(), _planes[i].model(), _planes[i].rg(), _planes[i].hp(), _planes[i].tw()) );
	}
}